package com.lineage.server.command.executor;

import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1KillNpc implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1KillNpc.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1KillNpc();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      if (pc.is_isKill()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "Kill Npc : Off"));
        pc.set_isKill(false);
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "Kill Npc : On"));
        pc.set_isKill(true);
        Kill kill = new Kill(pc);
        GeneralThreadPool.get().execute(kill);
      } 
    } catch (Exception e) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
  
  private class Kill implements Runnable {
    private final L1PcInstance _pc;
    
    private Kill(L1PcInstance pc) {
      this._pc = pc;
    }
    
    public void run() {
      try {
        while (this._pc.is_isKill()) {
          Thread.sleep(1000L);
          Iterator<L1Object> iterator = this._pc.getKnownObjects().iterator();
          while (iterator.hasNext()) {
            L1Object obj = iterator.next();
            if (obj instanceof L1MonsterInstance) {
              L1MonsterInstance mob = (L1MonsterInstance)obj;
              int hp = mob.getMaxHp() + 1000;
              mob.receiveDamage((L1Character)this._pc, hp);
            } 
          } 
        } 
      } catch (InterruptedException e) {
        L1KillNpc._log.error(e.getLocalizedMessage(), e);
      } 
    }
  }
}
